package com.all.play.myrecycleviewheadandfooter.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by 聪明一只哈 on 2018/5/21.
 * Time: 2018/5/21  17:43
 */

public class MyRecycleView extends RecyclerView {
    public MyRecycleView(Context context) {
        super(context);
    }

    public MyRecycleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }

    public MyRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private ArrayList<View> mHeaderViewInfo=new ArrayList<>();
    private ArrayList<View> mFooterViewInfo=new ArrayList<>();

    private Adapter mAdapter;

    public void addHeaderView(View v) {
        mHeaderViewInfo.add(v);

        if (mAdapter != null) {
            if (!(mAdapter instanceof HeaderViewRecycleViewAdapter)) {
                mAdapter=new HeaderViewRecycleViewAdapter(mHeaderViewInfo,mFooterViewInfo,mAdapter);
            }

        }
    }

    public void addFooterView(View v) {



        mFooterViewInfo.add(v);

        // Wrap the adapter if it wasn't already wrapped.
        if (mAdapter != null) {
            if (!(mAdapter instanceof HeaderViewRecycleViewAdapter)) {
                mAdapter=new HeaderViewRecycleViewAdapter(mHeaderViewInfo,mFooterViewInfo,mAdapter);
            }


        }
    }

    @Override
    public void setAdapter(Adapter adapter) {
        if (mHeaderViewInfo.size() > 0|| mFooterViewInfo.size() > 0) {
            mAdapter =new HeaderViewRecycleViewAdapter(mHeaderViewInfo, mFooterViewInfo, adapter);
        } else {
            mAdapter = adapter;
        }

        super.setAdapter(mAdapter);
    }
}
